﻿var userInfo;
$(document).ready(function () {
    LoadUserBaseInfo();
    IsShowTaskTipDiv = false;
    bgDivBackgroundColor = "#FCFCFC"
}); 

///加载会员基本信息；
function LoadUserBaseInfo() {
    AjaxGetEx("/AS/AjaxCommon.aspx?Action=GetUserBaseInfo" + "&t=" + Math.random(), null, function (user) {
        //用户基本信息对象；
        $("#spUserNameOfTop").html(user.UserName);
        $("#spShoppingCartCounts").html(user.ShoppingCartsCounts);
        $("#spShoppingCartsMoneys").html(user.ShoppingCartsMoneys);
        if (user.UserID > 0) {
            $("#liRegister").hide();
            $("#liLogin").hide();
        }
        else {
            $("#spLogout").hide();
        }
        userInfo = user;
    }, true)
}



///增加数量；
function AddQuantity(txtObj) {
    var quantity = parseInt($("#" + txtObj).val(), 10) + 1;
    $("#" + txtObj).val(quantity);
}

///减少数量；
function SubQuantity(txtObj) {
    var quantity = parseInt($("#" + txtObj).val(), 10);
    if (quantity > 1)
        quantity--;
    $("#" + txtObj).val(quantity);
}

///更改商品最终页的介绍导航
function ChangeGoodsNav(num) {
    for (var i = 0; i <= 3; ++i) {
        $("#GoodsNav" + i).attr("class", "");
        $("#divOfGoodsDesc" + i).css("display", "none");
    }
    $("#GoodsNav" + num).attr("class", "on_select");
    $("#divOfGoodsDesc" + num).css("display", "");
}

function ChangeGoodsImg(num) {
    $("#MainGoodsImg").attr("src", $("#GoodsImg" + num).attr("data"));
    $("#MainGoodsImg").attr("alt", $("#GoodsImg" + num).attr("alt"));
}

///加入购物车；
function AddCarts(goodsId, packUnitId, quantity, backUrl) {
     var url = "/Home/AS/ShoppingCartCommon.aspx?action=AddCart&GoodsID="+goodsId+"&PackUnitID="+packUnitId+"&Quantity="+quantity;
     AjaxGetEx(url, "", function (ret) {
         if (ret.Result == 1) {
             GetPopupDiv(400, 200, "#e2e2e2", "提示", function () {
                 LoadUserBaseInfo();
                 var html = new Array();
                 html.push("<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                 html.push("  <tr>");
                 html.push("    <td height=\"50\" align=\"center\"><img src=\"/images/AddCartSuc.gif\" width=\"304\" height=\"40\"></td>");
                 html.push("  </tr>");
                 html.push("  <tr>");
                 html.push("    <td align=\"center\">  &nbsp;&nbsp;<a href=\"javascript:yesOther(false);\"><img src=\"/images/gotobuy.gif\" width=\"104\" height=\"41\"></a>&nbsp;<a href=\"/Home/ConfirmOrder.aspx\"><img src=\"/images/jiesuan.gif\" width=\"104\" height=\"41\"></a></td>");
                 html.push("  </tr>");
                 html.push("</table>");

                 return html.join("");


             });
         }
         else {
             alert("系统提示：" + ret.Message);
         }
     }
    , true);

    //location.href = "/home/ShoppingCarts.aspx?GoodsID=" + goodsId + "&PackUnitID=" + packUnitId + "&Quantity=" + quantity + "&BackUrl=" + backUrl;
}





function LoadingShoppingCart() {
    var Action = "showshoppingcartCount";
    var success = function (ret) {
        $("#shoppingCartCount").html("购物车" + ret + "件");
    };
    var posttype = "GET";
    var datatype = "";
    publicAjaxPost(Action, "", success, posttype, datatype);
}
function LoadingUserStatus() {
    var Action = "ShowUserStatus";
    $(".unloginLi").hide();
    $(".loginedLi").hide();
    var success = function (ret) {
        eval("ret=" + ret);
        if (ret && ret._name) {
            $(".loginedLi").show();
            $("#topusername").html(ret._name);
        }
        else {
            $(".unloginLi").show();
        }
    };
    var posttype = "GET";
    var datatype = "JSON";
    publicAjaxPost(Action, "", success, posttype, datatype);
}


function publicAjaxPost(action, data, success1, posttype, datatype) {
    var url = "/as/AjaxCommon.aspx?Action=" + action + "&r=" + Math.random();

    var param = {
        type: posttype,
        url: url,
        data: data,
        success: success1
    };
    if (datatype) {
        param.datatype = datatype;
    }

    $.ajax(param);
}



//执行商品搜索；
function GoToSearch() {
    var url = "/Search.aspx?";
    url += "keyWords=" + escape($("#txtKeyWords").val().trim());
    location.href = url;
}

//为每个页面注册一个事件；
document.onkeydown = function (e) {
    var e = window.event ? window.event : e;
    var a = e.srcElement || e.target;
    if (e.keyCode == 13 && a.type != "textarea") {
        $("#btnSearch").click();
        e.cancelBubble = true;
        e.returnValue = false;
    }
}






function AddToFavorite(goodsId) {
    if (userInfo.UserID == 0) {
        alert("系统提示：您还没有登录，请先登录！");
        return;
    }
    var action = "AddFavorite";
    var data = "GoodsID=" + goodsId;
    var url = "/as/showcommon.aspx?Action=" + action + "&r=" + Math.random();
    $.ajax(
    {
        type: "get",
        url: url,
        data: data,
        success: function (ret) {
            eval("ret=" + ret);
            if (ret.Result == 1) {
                alert("该商品已经添加到关注列表,可以通过管理中的关注列表随时查看。");
            }
            else {

                if (!ret) {
                    alert("您还没有登录，请登录后关注该商品");
                    window.location.href = "/home/login.aspx";
                }
                else {
                    alert(ret.Message);
                }
            }
        }

    });
}




